//静态栈
#ifndef __STATICSTACK__HPP
#define __STATICSTACK__HPP

#include"VirtualStack.hpp"

namespace NgSiuKei {

template<class TheClass>
class StaticStack: public VirtualStack<TheClass> {
    public:
    StaticStack(void);
    ~StaticStack(void);

    public:
    //清空
    void clear(void);
    //获取栈顶元素
    TheClass getTopVal(void) const;
    //入栈
    void push(const TheClass val);
    //出栈
    TheClass pop(void);
    //打印栈
    void print(void) const;

    private:
    TheClass *p;
    U_L_L maxSize;//实际数组大小
};

template<class TheClass>
StaticStack<TheClass>::StaticStack(void): VirtualStack<TheClass>() {
    this->maxSize = MAX_SIZE;
    this->p = new TheClass[this->maxSize];
}

template<class TheClass>
StaticStack<TheClass>::~StaticStack(void) {
    delete this->p;
    this->p = nullptr;
}

template<class TheClass>
void StaticStack<TheClass>::clear(void) {
    this->size = 0;
}

template<class TheClass>
TheClass StaticStack<TheClass>::getTopVal(void) const {
    if(this->isEmpty()) {
        throw EXCEPTION_IS_EMPTY;
    }
    return this->p[this->size-1];
}

template<class TheClass>
void StaticStack<TheClass>::push(const TheClass val) {
    if(nullptr == this->p) {
        this->maxSize = MAX_SIZE;
        this->p = new TheClass[this->maxSize];
    }

    //检查是否需要扩容
    if(this->size == this->maxSize) {
        this->maxSize += MAX_SIZE;
        TheClass *newArray = new TheClass[this->maxSize];
        for(U_L_L i=0; i<this->size; ++i) {
            newArray[i] = this->p[i];
        }
        delete this->p;
        this->p = newArray;
    }

    this->p[this->size] = val;
    this->size++;
}

template<class TheClass>
TheClass StaticStack<TheClass>::pop(void) {
    if(this->isEmpty()) {
        throw EXCEPTION_IS_EMPTY;
    }
    this->size--;
    return this->p[this->size];
}

template<class TheClass>
void StaticStack<TheClass>::print(void) const {
    cout << "打印静态栈：[ ";
    for(U_L_L i=0; i<this->size; ++i) {
        cout << this->p[i] << ' ';
    }
    cout << ']' << endl;
}

}

#endif